package com.jxwifi.cloud.quickclean.main;

import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.net.Uri;
import android.os.Handler;
import android.support.design.widget.NavigationView;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.adapter.FunctionAdapter;
import com.jxwifi.cloud.quickclean.adapter.TabPageAdapter;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.Host;
import com.jxwifi.cloud.quickclean.app.LocationGaoDe;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.assemble.AssembleOrderActivity;
import com.jxwifi.cloud.quickclean.bean.AddPreOrderResVoBean;
import com.jxwifi.cloud.quickclean.bean.AdvertBean;
import com.jxwifi.cloud.quickclean.bean.IndexAsynDataBean;
import com.jxwifi.cloud.quickclean.bean.InfoBean;
import com.jxwifi.cloud.quickclean.bean.NavigationBean;
import com.jxwifi.cloud.quickclean.bean.ParameterBean;
import com.jxwifi.cloud.quickclean.bean.ProductsBean;
import com.jxwifi.cloud.quickclean.card.CardRollActivity;
import com.jxwifi.cloud.quickclean.login.BindPhoneActivity;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.mycenter.AboutWeActivity;
import com.jxwifi.cloud.quickclean.mycenter.GradeRankActivity;
import com.jxwifi.cloud.quickclean.mycenter.PersonalActivity;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.order.ConfirmOrderActivity;
import com.jxwifi.cloud.quickclean.order.MyOrderActivity;
import com.jxwifi.cloud.quickclean.orderdetails.OrderDetailsActivity;
import com.jxwifi.cloud.quickclean.popwindow.PopShowDialogModule;
import com.jxwifi.cloud.quickclean.quicktreasure.QuickTreasureActivity;
import com.jxwifi.cloud.quickclean.utils.DetectionUtils;
import com.jxwifi.cloud.quickclean.utils.GlideImageLoader;
import com.jxwifi.cloud.quickclean.utils.RouteManager;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.jxwifi.cloud.quickclean.utils.TranslateUriUtils;
import com.popwindow.PopWindowUtil;
import com.popwindow.Popwindows;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.analytics.MobclickAgent;
import com.view.SuperSwipeRefreshLayout;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import cn.trinea.android.common.util.ListUtils;

public class MainPresenter {

    private String TAG = this.getClass().getSimpleName();
    private MainView mView;
    private Context mContext;
    private AuzImpl mImpl;

    private List<ProductsBean> mProductsBean;
    private IndexAsynDataBean mBean;
    //private String[] mTabNames;
    private String mServiceCode;

    private ProgressBar progressBar;
    private TextView textView;
    private ImageView imageView;

    private String area;
    private String city;
    private String province;
    private String mCustomer;//客服電話
    private int mTagsPos;
    private TabPageAdapter pageAdapter;

    private String mAddressid; //地址id
    private String mProductPriceCode;//产品定价编号
    private String mSpeceCode;//产品标签编号

    private int mAreaindex = 0;//面积选择后默认状态标识

    private ArrayList<String> list_images_path;

    private FunctionAdapter mAdapter;

    private List<AdvertBean> mListAdvert;//广告集合

    private String[] ProductName = {"工具包", "清洁券", "邀请有礼", "推荐有礼"};
    private int[] ProductImg = {R.mipmap.gongjubao, R.mipmap.iconfont4, R.mipmap.iconfont5, R.mipmap.iconfont6};

    public MainPresenter(Context context, MainView View) {
        this.mView = View;
        this.mContext = context;
        mImpl = new AuzImpl();
    }

    //首页数据获取
    public void getdata() {
        Params params = new Params();
        Log.w("oushuhua", "province" + LocationGaoDe.getProvince() + "city" + LocationGaoDe.getCity() + "area" + LocationGaoDe.getArea());
        params.add("area", area);//area
        params.add("city", city);//city
        params.add("province", province);//province
        mImpl.auzpost(UrlPool.GET_INDEX, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code == 0) {
                    //Log.i(TAG,"getdata() ==> data = "+data);
                    JSONObject obj = JSON.parseObject(data);
                    String str = obj.getString("products");
                    //Log.i(TAG,"getdata() ==> str = "+str);
                    mServiceCode = obj.getString("serviceCode");
                    mProductsBean = JSON.parseArray(str, ProductsBean.class);
                    getAsynData();
                    mView.onAfterDo();
                } else {
                    mView.ondata();
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

    //首页异步数据获取
    public void getAsynData() {
        Params params = new Params();
        params.add("prices", mView.getEstimateMoney());
        params.add("serviceCode", mServiceCode);
        mImpl.auzpost(UrlPool.INDEX_ASYN_DATA, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code == 0) {
                    mBean = JSON.parseObject(data, IndexAsynDataBean.class);
                    Elastic();
                    //Available();
                    //隐藏与显示进行中的订单
                    //Log.i(TAG,"VIP等级 = "+mBean.getKjbGradeDictionary().getGradeName());
                    if(null != mBean.getKjbGradeDictionary()){
                        AccountMgr.putGradeName(mContext,mBean.getKjbGradeDictionary().getGradeName());
                        AccountMgr.putGradeVip(mContext,mBean.getKjbGradeDictionary().getGradeVip());
                        mView.getGradeName().setText(AccountMgr.getGradeVip(mContext)+AccountMgr.getGradeName(mContext));
                    }
                    if (mBean != null) {
                        if (!ListUtils.isEmpty(mBean.getConfigs())) {
                            for (int i = 0; i < mBean.getConfigs().size(); i++) {
                                if ("kjb_td_order_timeout_handle".equals(mBean.getConfigs().get(i).getKeyName())) {
                                    AccountMgr.putOrderTimeOut(mContext,Integer.parseInt(mBean.getConfigs().get(i).getKeyValue()));
                                    break;
                                }
                            }
                        }
                    }
                    mView.setMessageNum(mBean.getUnreadMessages());
                    //Log.i(TAG,"未读消息 = "+mBean.getUnreadMessages());
                    //Log.i(TAG,"VIP等级 = "+mBean.getKjbGradeDictionary().getGradeName()+"AccountMgr getGradeName = "+AccountMgr.getGradeName(mContext)+" getGradeVip = "+AccountMgr.getGradeVip(mContext));
                    if (!ListUtils.isEmpty(mBean.getIngorders())) {
                        mView.getOrderDoing().setVisibility(View.VISIBLE);
                        mView.getOrderDoingText().setText("您有" + (mBean.getIngorders().size()) + "个订单进行中");
                    } else {
                        mView.getOrderDoing().setVisibility(View.GONE);
                        mView.getOrderDoingText().setText("您有未结算订单");
                    }
                } else {
                    mView.onAsynData();
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

    //用户信息
    public void getinfo() {
        Params params = new Params();
        params.add("customerCode", AccountMgr.getCode(mContext));
        mImpl.auzget(UrlPool.GET_INFO, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                if (code == 0) {
                    InfoBean minfoBean = JSON.parseObject(data, InfoBean.class);
                    AccountMgr.putUser(mContext, minfoBean, true);

                    mView.onAddressBean(minfoBean.getDefaultAddress());
                    //如果地址为空获取当前位置信息,用于做首页的数据请求
                    if (minfoBean.getDefaultAddress() != null) {
                        if (minfoBean.getDefaultAddress().getArea() == null || "null".equals(minfoBean.getDefaultAddress().getCity())
                                || TextUtils.isEmpty(minfoBean.getDefaultAddress().getProvince())) {
                            area = LocationGaoDe.getArea();
                            city = LocationGaoDe.getCity();
                            province = LocationGaoDe.getProvince();
                        } else {
                            area = minfoBean.getDefaultAddress().getArea();
                            city = minfoBean.getDefaultAddress().getCity();
                            province = minfoBean.getDefaultAddress().getProvince();
                            mAddressid = minfoBean.getDefaultAddress().getId();
                        }
                    } else {
                        area = LocationGaoDe.getArea();
                        city = LocationGaoDe.getCity();
                        province = LocationGaoDe.getProvince();
                    }
                    getdata();
                } else {
                    mView.oninfo();
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });

    }

    //初始化侧滑菜单
    public void Sideslip() {
        if (!TextUtils.isEmpty(AccountMgr.getHeadImg(mContext))) {
            Glide.with(mContext).load(AccountMgr.getHeadImg(mContext)).error(R.mipmap.icon_default_head).dontAnimate().into(mView.getImgHead());
        } else {
            Glide.with(mContext).load(R.mipmap.icon_default_head).dontAnimate().into(mView.getImgHead());
        }
        mView.getName().setText(AccountMgr.getName(mContext));
        mView.getNav().setItemTextColor(ColorStateList.valueOf(Color.parseColor("#666666")));
        mView.getNav().setItemIconTintList(null);
        mView.getNav().setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                Intent intent = new Intent();
                switch (menuItem.getItemId()) {

                    case R.id.my_card_account: //我的账户
                        intent.setClass(mContext, QuickTreasureActivity.class);
                        mContext.startActivity(intent);
                        MobclickAgent.onEvent(mContext, "main_my_card_account");
                        break;
                    case R.id.my_card_roll:
                        if (!TextUtils.isEmpty(AccountMgr.gettell(mContext))) {
                            intent.setClass(mContext, CardRollActivity.class);
                            intent.putExtra("parameters", StateUtils.MAIN_COME);
                            mContext.startActivity(intent);
                            MobclickAgent.onEvent(mContext, "main_my_card_roll");
                        } else {
                            intent.setClass(mContext, BindPhoneActivity.class);
                            mContext.startActivity(intent);
                        }
                        break;
                    case R.id.my_order:
                        if (!TextUtils.isEmpty(AccountMgr.gettell(mContext))) {
                            intent.setClass(mContext, MyOrderActivity.class);
                            mContext.startActivity(intent);
                            MobclickAgent.onEvent(mContext, "main_my_order");
                        } else {
                            intent.setClass(mContext, BindPhoneActivity.class);
                            mContext.startActivity(intent);
                        }
                        break;
                        //我的拼团
                    case R.id.my_assemble_order:
                        if (!TextUtils.isEmpty(AccountMgr.gettell(mContext))) {
                            intent.setClass(mContext, AssembleOrderActivity.class);
                            mContext.startActivity(intent);
                            MobclickAgent.onEvent(mContext, "main_my_order");
                        } else {
                            intent.setClass(mContext, BindPhoneActivity.class);
                            mContext.startActivity(intent);
                        }
                        break;
                    case R.id.user_protocol:
                        intent.setClass(mContext, MainUserActivity.class);
                        mContext.startActivity(intent);
                        MobclickAgent.onEvent(mContext, "main_user_protocol");
                        break;
                    case R.id.contact_customer_service:
                        CustomerService();
                        break;
                   /* case R.id.invite_awards:
                        Toast.makeText(mContext, "功能有待开发，敬请期待", Toast.LENGTH_SHORT).show();
                        break;*/
                    case R.id.about_we:
                        intent.setClass(mContext, AboutWeActivity.class);
                        mContext.startActivity(intent);
                        MobclickAgent.onEvent(mContext, "main_about_we");
                        break;

                }
                mView.getDrawer().closeDrawers();
                return false;
            }
        });
        mView.getImgHead().setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, PersonalActivity.class);
                mContext.startActivity(intent);
                mView.getDrawer().closeDrawers();
            }
        });
        //等级排行
        mView.getGradeDescription().setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, GradeRankActivity.class);
                mContext.startActivity(intent);
                mView.getDrawer().closeDrawers();
            }
        });
        //等级说明
        mView.getImgGradeDescription().setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mView.showGradeDescription();
            }
        });


        mView.getSwip().setHeaderView(createHeaderView());
        mView.getSwip().setOnPullRefreshListener(new SuperSwipeRefreshLayout.OnPullRefreshListener() {

            @Override
            public void onRefresh() {
                textView.setText("正在刷新");
                imageView.setVisibility(View.GONE);
                progressBar.setVisibility(View.VISIBLE);
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        mView.getSwip().setRefreshing(false);
                        progressBar.setVisibility(View.GONE);
                        getinfo();
                    }
                }, 1000);
            }

            @Override
            public void onPullDistance(int distance) {
                // pull distance
            }

            @Override
            public void onPullEnable(boolean enable) {
                textView.setText(enable ? "松开刷新" : "下拉刷新");
                imageView.setVisibility(View.VISIBLE);
                imageView.setRotation(enable ? 180 : 0);
            }
        });
    }

    private View createHeaderView() {
        View headerView = LayoutInflater.from(mView.getSwip().getContext())
                .inflate(R.layout.layout_head, null);
        progressBar = (ProgressBar) headerView.findViewById(R.id.pb_view);
        textView = (TextView) headerView.findViewById(R.id.text_view);
        textView.setText("下拉刷新");
        imageView = (ImageView) headerView.findViewById(R.id.image_view);
        imageView.setVisibility(View.VISIBLE);
        imageView.setImageResource(R.drawable.down_arrow);
        progressBar.setVisibility(View.GONE);
        return headerView;
    }

    //获取首页广告数据请求
    public void initAdvert() {
        Params params = new Params();
        Log.w("oushuhua", "province" + LocationGaoDe.getProvince() + "city" + LocationGaoDe.getCity() + "area" + LocationGaoDe.getArea());
        params.add("area", area);//area
        params.add("city", city);//city
        params.add("province", province);//province
        mImpl.auzpost(UrlPool.POST_INDEX_ADVERT, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                //AdvertBean
                if (mListAdvert!=null){
                    mListAdvert.clear();
                }
                mListAdvert = JSON.parseArray(data, AdvertBean.class);
                initBanner();
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

    //初始化轮播
    public void initBanner() {
        //放图片地址的集合
        list_images_path = new ArrayList<>();
        if (mListAdvert.size()>0){
            for (int i = 0; i < mListAdvert.size(); i++) {
                list_images_path.add(mListAdvert.get(i).getImageUrl());
            }
        }else{
            //如果数据为空添加两张默认图
            list_images_path.add("https://kjb-oss.oss-cn-shenzhen.aliyuncs.com/ul9zg740a645wxjfxddl.png");
            list_images_path.add("https://kjb-oss.oss-cn-shenzhen.aliyuncs.com/h3xjask0xyj7bclwdzwv.png");
        }
        //设置banner样式
        mView.getBanner().setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        mView.getBanner().setImageLoader(new GlideImageLoader());
        //设置图片集合
        mView.getBanner().setImages(list_images_path).setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int i) {
                if (mListAdvert.size()>0){
                    Intent intent = new Intent();
                    if (StateUtils.ADVERT_NATIVE.equals(mListAdvert.get(i).getPositionType())) { //转跳原生
                        if (!TextUtils.isEmpty(mListAdvert.get(i).getPositionUrl())&&mListAdvert.get(i).getPositionUrl()!=null) {
                            if (!TextUtils.isEmpty(mListAdvert.get(i).getParameter())){
                                ParameterBean mBean = JSON.parseObject(mListAdvert.get(i).getParameter(), ParameterBean.class);
                                RouteManager.startActivity(mContext, mListAdvert.get(i).getPositionUrl(), mBean);
                            }else{
                                RouteManager.startActivity(mContext, mListAdvert.get(i).getPositionUrl(), new ParameterBean());
                            }
                        }
                    } else if (StateUtils.ADVERT_URL.equals(mListAdvert.get(i).getPositionType())) { //转跳到URL
                        if (!TextUtils.isEmpty(mListAdvert.get(i).getPositionUrl())&&mListAdvert.get(i).getPositionUrl()!=null) {
                            intent.setClass(mContext, WebViewsActivity.class);
                            intent.putExtra("web_view_url", mListAdvert.get(i).getPositionUrl());
                            intent.putExtra("web_view_title", mListAdvert.get(i).getTitle());
                            mContext.startActivity(intent);
                        }
                    }
                    MobclickAgent.onEvent(mContext, "main_advertisement");

                }
            }
        });
        //设置banner动画效果
        mView.getBanner().setBannerAnimation(Transformer.Default);
        //设置自动轮播，默认为true
        mView.getBanner().isAutoPlay(true);
        //设置轮播时间
        mView.getBanner().setDelayTime(5000);
        //设置指示器位置（当banner模式中有指示器时）
        mView.getBanner().setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        mView.getBanner().start();
    }

    //加载功能模块
    public void function(List<NavigationBean> mListBean,DisplayMetrics dm) {
        mView.getRecyclerView().setLayoutManager(new GridLayoutManager(mContext, 3));
        if (mProductsBean == null||mProductsBean.size()==0) {
            NavigationBean mProduct = new NavigationBean();
            mProduct.setTitle("日常保洁");
            mProduct.setImageUrl(TranslateUriUtils.imageTranslateUri(mContext, R.mipmap.iconfont1));
            mListBean.add(0, mProduct);
            NavigationBean mProduct1 = new NavigationBean();
            mProduct1.setTitle("开荒保洁");
            mProduct.setImageUrl(TranslateUriUtils.imageTranslateUri(mContext, R.mipmap.iconfont1));
            mListBean.add(1, mProduct1);
        } else {
            for (int i = 0; i < mProductsBean.size(); i++) {
                NavigationBean mProduct = new NavigationBean();
                mProduct.setTitle(mProductsBean.get(i).getProductName());
                mProduct.setImageUrl(mProductsBean.get(i).getIcoImgUrl());
                mListBean.add(i, mProduct);
            }
        }
        mView.getRecyclerView().setAdapter(mAdapter = new FunctionAdapter(R.layout.main_function_items, mListBean,dm));
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                //Log.i(TAG,"getPositionType = "+mAdapter.getData().get(position).getPositionType()+" getPositionUrl = "+mAdapter.getData().get(position).getPositionUrl());
                if (!TextUtils.isEmpty(mAdapter.getData().get(position).getPositionType())) {
                    if (!TextUtils.isEmpty(mAdapter.getData().get(position).getPositionUrl())) {
                        Intent intent = new Intent();
                        if (StateUtils.ADVERT_NATIVE.equals(mAdapter.getData().get(position).getPositionType())) { //转跳原生
                            if (mAdapter.getData().get(position).getPositionUrl().toString().contains(".")) {
                                ParameterBean mBean = JSON.parseObject(mAdapter.getData().get(position).getParameter(), ParameterBean.class);
                                RouteManager.startActivity(mContext, mAdapter.getData().get(position).getPositionUrl(), mBean);
                            }else{
                                if(StateUtils.INVITATION.equals(mAdapter.getData().get(position).getPositionUrl())){//邀请有礼
                                    ToWXAPI(1);
                                }else if(StateUtils.RECOMMEND.equals(mAdapter.getData().get(position).getPositionUrl())){//推荐有礼
                                    ToWXAPI(2);
                                }
                            }
                        } else if (StateUtils.ADVERT_URL.equals(mAdapter.getData().get(position).getPositionType())) { //转跳到URL
                            if (!TextUtils.isEmpty(mAdapter.getData().get(position).getPositionUrl().toString())) {
                                intent.setClass(mContext, WebViewsActivity.class);
                                intent.putExtra("web_view_url", mAdapter.getData().get(position).getPositionUrl());
                                intent.putExtra("web_view_title", mAdapter.getData().get(position).getTitle());
                                mContext.startActivity(intent);
                            }
                        }
                    }
                } else { //开荒保洁与日常保洁跳转方法
                    if (TextUtils.isEmpty(mProductsBean.get(position).getServicePrice().getProductPriceCode()) || "".equals(mProductsBean.get(position).getServicePrice().getProductPriceCode())) {
                        Toast toast = Toast.makeText(mContext, "服务暂未开通", Toast.LENGTH_SHORT);
                        ToastUtils.showMyToast(toast, 3000);
                    } else {
                        PlaceOrder(position);
                    }
                }
            }
        });
    }

    //九宫格接口
    public void navigation(final DisplayMetrics dm) {
        Params params = new Params();
        Log.w("oushuhua", "province" + LocationGaoDe.getProvince() + "city" + LocationGaoDe.getCity() + "area" + LocationGaoDe.getArea());
        params.add("area", area);//area
        params.add("city", city);//city
        params.add("province", province);//province
        mImpl.auzpost(UrlPool.POST_NAVIGATION, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                //Log.i(TAG,"navigation ==> data = "+data);
                List<NavigationBean> mListBean = JSON.parseArray(data, NavigationBean.class);
                function(mListBean,dm);
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }

    //跳转小程序
    public void ToWXAPI(int indexNum) {
        //Log.i(TAG,"ToWXAPI ==> indexNum = "+indexNum);
        if(DetectionUtils.isAvilible(mContext, StateUtils.WX_TENCENT)){
            IWXAPI api = WXAPIFactory.createWXAPI(mContext, StateUtils.WX_APPID);
            WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
            req.userName = StateUtils.WEIXIN_XIAOCHENGXU_ID; // 填小程序原始id
            if (indexNum == 1) {
                req.path = "/pages/share"; //拉起小程序页面的可带参路径，不填默认拉起小程序首页
            } else if (indexNum == 2) {
                req.path = "/pages/invite"; //拉起小程序页面的可带参路径，不填默认拉起小程序首页
            }
            // 可选打开 测试版：MINIPROGRAM_TYPE_TEST，体验版:MINIPROGRAM_TYPE_PREVIEW 和正式版:MINIPTOGRAM_TYPE_RELEASE
            if (StateUtils.HOST_ON_LINE.equals(Host.HOST)){
                req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;// 正式版:0，测试版:1，体验版:2
            }else{
                req.miniprogramType = WXLaunchMiniProgram.Req.MINIPROGRAM_TYPE_PREVIEW;//体验版:2
            }
            api.sendReq(req);
        }else{
            Toast toast = Toast.makeText(mContext, "您还未安装微信客户端", Toast.LENGTH_SHORT);
            ToastUtils.showMyToast(toast, 3000);
        }

    }

    //首页广告弹框
    public void Elastic() {
        if (!ListUtils.isEmpty(mBean.getCoupons())) {
            //PopShowDialogModule
            DisplayMetrics dm = new DisplayMetrics();
            mView.getManager().getDefaultDisplay().getMetrics(dm);
            PopShowDialogModule mPopModule = new PopShowDialogModule(mContext, mBean.getCoupons().get(0).getCoupon().getBackground(), dm, mBean.getCoupons().size(), new PopShowDialogModule.popDialogListener() {
                @Override
                public void dialogClickTv() {
                    //.startA跳转到卡卷列表ctivity(mContext, "");
                    Intent intent = new Intent();
                    intent.setClass(mContext, CardRollActivity.class);
                    intent.putExtra("parameters", StateUtils.MAIN_COME);
                    mContext.startActivity(intent);
                }

                @Override
                public void dialogDismiss() {
                    //initHeadLayout();
                }
            });
            Popwindows window = PopWindowUtil.getPopupWindow(mPopModule.getView(), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup
                    .LayoutParams.MATCH_PARENT);
            mPopModule.setPopwindow(window);
            window.show(mView.getDrawer(), Popwindows.Location.TOP);
        }
    }

    //联系客服
    public void CustomerService() {
        if (mBean != null) {
            if (!ListUtils.isEmpty(mBean.getConfigs())) {
                for (int i = 0; i < mBean.getConfigs().size(); i++) {
                    if ("kjb_ke_tell".equals(mBean.getConfigs().get(i).getKeyName())) {
                        mCustomer = mBean.getConfigs().get(i).getKeyValue();
                        break;
                    }
                }
                if (!TextUtils.isEmpty(mCustomer)) {
                    Intent mTelintent = new Intent(Intent.ACTION_DIAL);
                    Uri Urldata = Uri.parse("tel:" + mCustomer.replace("\\", ""));
                    mTelintent.setData(Urldata);
                    mContext.startActivity(mTelintent);
                }
            }
        }

    }

    //进行中的订单
    public void OngoingOrders() {
        if (mBean.getIngorders().size() > 1) { //进行中的订单大于一个的时候跳转订单列表
            Intent intent = new Intent();
            intent.setClass(mContext, MyOrderActivity.class);
            mContext.startActivity(intent);

        } else if (mBean.getIngorders().size() == 1) { //进行中的订单只有一个的时候跳转详情页面
            Intent intent = new Intent();
            intent.setClass(mContext, OrderDetailsActivity.class);
            intent.putExtra("order_code", mBean.getIngorders().get(0).getOrderCode());
            mContext.startActivity(intent);
        }
    }


    //立即下单
    public void PlaceOrder(final int postions) {
        Params params = new Params();
        params.add("addressId", mAddressid);
        params.add("productCode",mProductsBean.get(postions).getProductCode());
        mImpl.auzpost(UrlPool.POST_ORDER_V2_ADD_PREORDER, params, new AuzImpl.AuzListener() {
            @Override
            public void AuzSuccess(int code, String data) {
                //AddPreOrderResVoBean
                if (code == 0) {
                    AddPreOrderResVoBean mAddPreOrderResVo = JSON.parseObject(data, AddPreOrderResVoBean.class);
                    mAddPreOrderResVo.setTagCode(mSpeceCode);
                    mAddPreOrderResVo.setProductPriceCode(mProductPriceCode);
                    Intent intent = new Intent();
                    intent.setClass(mContext, ConfirmOrderActivity.class);
                    intent.putExtra("preorder_resvo", mAddPreOrderResVo);
                    if (mProductsBean.get(postions).getServicePrice() != null) {
                        //新增代码2020-03-19 判断是否有拼团
                        if (!TextUtils.isEmpty(mAddPreOrderResVo.getAssembleOrderCode())){
                            intent.putExtra("preorder_pirce", mAddPreOrderResVo.getAssemblePrice());
                        }else{
                            intent.putExtra("preorder_pirce", mProductsBean.get(postions).getServicePrice().getPrice());
                        }
                    }

                    intent.putExtra("preorder_products", mProductsBean.get(postions));
                    mContext.startActivity(intent);
                } else {
                    mView.onPlaceOrder(postions);
                }
            }

            @Override
            public void AuzFailure(int errorCode, String msg) {
                mView.onFailure(errorCode, msg);
            }
        });
    }


}
